package com.example.springbootsfzy.controller.sunnan;

import com.example.springbootsfzy.domain.SelectedArticles;
import com.example.springbootsfzy.service.sunnan.SelectedArticleService;
import com.example.springbootsfzy.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/articles")
public class SelectedArticlesController {

    @Autowired
    private SelectedArticleService selectedArticleService;

    /**
     * 分页查询所有文章
     * @param pageNum 当前页码
     * @param pageSize 每页显示的数量
     * @return PageInfo
     */
    @GetMapping
    @ResponseBody
    public PageInfo<SelectedArticles> getArticles(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return selectedArticleService.getArticles(pageNum, pageSize);
    }
    @GetMapping("/{articleId}")
    @ResponseBody
    public R getArticle(@PathVariable int articleId) {
        return R.ok(selectedArticleService.getArticle(articleId));
    }
    /**
     * 添加文章
     * @param selectedArticles 文章对象
     */
    @PostMapping
    @ResponseBody
    public R addArticle(@RequestBody SelectedArticles selectedArticles) {
        if (selectedArticleService.addArticle(selectedArticles) > 0) {
            return R.ok(null);
        } else {
            return R.fail("添加失败");
        }
    }
    /**
     * 根据id删除文章
     * @param articleId 文章id
     *
     */
    @DeleteMapping("/{articleId}")
    public R deleteById(@PathVariable int articleId) {
        if (selectedArticleService.deleteById(articleId) > 0) {
            return R.ok(null);
        } else {
            return R.fail("修改失败");
        }
    }

    /**
     * 根据id更新文章
     * @param selectedArticles 文章对象
     */
    @PutMapping
    @ResponseBody
    public R updateById(@RequestBody SelectedArticles selectedArticles) {
        if (selectedArticleService.updateBySelectedArticles(selectedArticles) > 0) {
            return R.ok(null);
        } else {
            return R.fail("修改失败");
        }}
}
